package net.ecpt.crm.services;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilDateTime;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.entity.Delegator;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.condition.EntityCondition;
import org.ofbiz.entity.condition.EntityOperator;
import org.ofbiz.entity.util.EntityUtil;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.ServiceUtil;
import org.ofbiz.wvtools.utils.CommonUtils;

import javolution.util.FastList;
import javolution.util.FastMap;

public class SmsReportServices {
	
	
	 public static final String module = SmsReportServices.class.getName();

//	活跃商机
	public static Map<String, Object> getActiveBusinessReport(DispatchContext dctx,
		    Map<String, ? extends Object> context) {
		Map<String, Object> result = ServiceUtil.returnSuccess();
		Delegator delegator = dctx.getDelegator();
		GenericValue userLogin = (GenericValue) context.get("userLogin");
		Debug.log("getActiveBusinessReport == start");
		String ownerUserId = userLogin.getString("userLoginId");
		
		

		// 所属bu
		List<String> baseIdList = new ArrayList();
		try {
		    String personTypeId = (String) context.get("personTypeId");
		    Timestamp createTime_fld0_value = (Timestamp) context.get("createTime_fld0_value");
		    Timestamp createTime_fld1_value = (Timestamp) context.get("createTime_fld1_value");
		    if (UtilValidate.isEmpty(createTime_fld0_value)) {
			createTime_fld0_value = CommonUtils.getLastYear(-2);
		    }
		    if (UtilValidate.isEmpty(createTime_fld1_value)) {
			createTime_fld1_value = UtilDateTime.nowTimestamp();
		    }
//		    List ownerUserIdList = new ArrayList();
		    if (UtilValidate.isEmpty(personTypeId)) {
			personTypeId = "2";
		    }
		    if ("2".equals(personTypeId)) {// 本bu
			String buCode = CrmReportServices.getBu(delegator, ownerUserId, "N");
			baseIdList.add(buCode);
		    } else if ("allData".equals(personTypeId)) { // 全国
			List<GenericValue> buList = delegator.findByAnd("DataDictionary",
				UtilMisc.toMap("dictionaryTypeId", "BU_DEPARTMENT"), UtilMisc.toList("sequenceId"), false);
			x: for (GenericValue bu : buList) {
			    String dictionaryCode2 = bu.getString("dictionaryCode2");
			    if ("N".equals(dictionaryCode2)) {
				continue x;
			    }
			    baseIdList.add(bu.getString("dictionaryCode"));
			}

		    } else if ("3".equals(personTypeId)) { // 本大区
			// 先找到人属于那个bu.找到bu属于哪个大区.
			List<GenericValue> shipList = delegator.findByAnd("CoRelationship",
				UtilMisc.toMap("partyIdTo", ownerUserId), null, false);
			if (UtilValidate.isNotEmpty(shipList)) {
			    // 获取buCode
			    String buCode = EntityUtil.getFirst(shipList).getString("partyIdFrom");
			    List<GenericValue> dataList1 = delegator.findByAnd("DataDictionary",
				    UtilMisc.toMap("dictionaryCode", buCode), null, false);

			    GenericValue data1 = EntityUtil.getFirst(dataList1);
			    String areaId = data1.getString("parentId");
			    // 获取大区ID
			    if (UtilValidate.isNotEmpty(areaId)) {
				// 获取大区下的bu列表
				List<GenericValue> sunBuList = delegator.findByAnd("DataDictionary",
					UtilMisc.toMap("dictionaryTypeId", "BU_DEPARTMENT", "parentId", areaId), null, false);
				x: for (GenericValue bu : sunBuList) {
				    String dictionaryCode2 = bu.getString("dictionaryCode2");
				    if ("N".equals(dictionaryCode2)) {
					continue x;
				    }
				    baseIdList.add(bu.getString("dictionaryCode"));
				}
			    }

			}
		    } else {
			List<GenericValue> dataList = delegator.findByAnd("DataDictionary",
				UtilMisc.toMap("dictionaryCode", personTypeId), null, false);
			GenericValue data = EntityUtil.getFirst(dataList);
			String dictionaryTypeId = data.getString("dictionaryTypeId");
			// 各大区
			if (dictionaryTypeId.equals("AREA_DEPARTMENT")) {
			    List<GenericValue> childList = delegator.findByAnd("DataDictionary",
				    UtilMisc.toMap("parentId", data.getString("dictionaryId")), null, false);
			    for (GenericValue child : childList) {
				String code = child.getString("dictionaryCode");
				baseIdList.add(code);
			    }
			} else {
			    // 各bu
			    baseIdList.add(personTypeId);
			}
		    }

		    EntityCondition mainCond = null;
		    List<EntityCondition> andExprs = FastList.newInstance();
		    if (UtilValidate.isNotEmpty(baseIdList)) {
			andExprs.add(EntityCondition.makeCondition("ownerBuId", EntityOperator.IN, baseIdList));
		    }
		    andExprs.add(EntityCondition.makeCondition("statusId", EntityOperator.EQUALS, "Y"));
		    andExprs.add(EntityCondition.makeCondition("removed", EntityOperator.EQUALS, "N"));

		    if (andExprs.size() > 0) {
			mainCond = EntityCondition.makeCondition(andExprs, EntityOperator.AND);
		    }
		    // 按仓库统计 销售情况
		    List salesDataListByWarehouse = new ArrayList();

		    // 销售预警
		    List cautionSalesDataListByWarehouse = new ArrayList();

		    List orderBy = UtilMisc.toList("warehouseCode");
		    List<GenericValue> entityList = delegator.findList("CrmWmsInventory", mainCond, null, orderBy, null, true);

		    for (GenericValue entity : entityList) {
			Map<String, Object> map = FastMap.newInstance();
			Map<String, Object> cautionSalesDataMap = FastMap.newInstance();

			String id = entity.getString("id");
			// String ownerBuId = entity.getString("ownerBuId");
			String warehouseName = entity.getString("warehouseName");
			// 建设冷冻容量
			BigDecimal buildingFreeze = entity.getBigDecimal("buildingFreeze");
			buildingFreeze = UtilValidate.isEmpty(buildingFreeze) ? BigDecimal.ZERO : buildingFreeze;
			// 已使用冷冻
			BigDecimal physicalFreeze = entity.getBigDecimal("physicalFreeze");
			physicalFreeze = UtilValidate.isEmpty(physicalFreeze) ? BigDecimal.ZERO : physicalFreeze;
			// 剩余量
			// BigDecimal availableFreeze = buildingFreeze.subtract(physicalFreeze);

			// 建设冷藏容量
			BigDecimal buildingRefrigerate = entity.getBigDecimal("buildingRefrigerate");
			buildingRefrigerate = UtilValidate.isEmpty(buildingRefrigerate) ? BigDecimal.ZERO : buildingRefrigerate;
			// 已使用冷藏
			BigDecimal physicalRefrigerate = entity.getBigDecimal("physicalRefrigerate");
			physicalRefrigerate = UtilValidate.isEmpty(physicalRefrigerate) ? BigDecimal.ZERO : physicalRefrigerate;
			// BigDecimal availableRefrigerate =
			// buildingRefrigerate.subtract(physicalRefrigerate);

			// 建设恒温容量
			BigDecimal buildingConstant = entity.getBigDecimal("buildingConstant");
			buildingConstant = UtilValidate.isEmpty(buildingConstant) ? BigDecimal.ZERO : buildingConstant;
			// 已使用恒温
			BigDecimal physicalConstant = entity.getBigDecimal("physicalConstant");
			physicalConstant = UtilValidate.isEmpty(physicalConstant) ? BigDecimal.ZERO : physicalConstant;
			// BigDecimal availableConstant = buildingConstant.subtract(physicalConstant);

			// 建设常温容量
			BigDecimal buildingNormal = entity.getBigDecimal("buildingNormal");
			buildingNormal = UtilValidate.isEmpty(buildingNormal) ? BigDecimal.ZERO : buildingNormal;
			// 已使用常温
			BigDecimal physicalNormal = entity.getBigDecimal("physicalNormal");
			physicalNormal = UtilValidate.isEmpty(physicalNormal) ? BigDecimal.ZERO : physicalNormal;
			// BigDecimal availableNormal = buildingNormal.subtract(physicalNormal);

			BigDecimal buildingTotal = buildingFreeze.add(buildingRefrigerate).add(buildingConstant)
				.add(buildingNormal);

			// 已用库存小计
			BigDecimal physicalTotal = physicalFreeze.add(physicalRefrigerate).add(physicalConstant)
				.add(physicalNormal);

			// 可用库存小计
			BigDecimal availableTotal = buildingTotal.subtract(physicalTotal);

			Map estimatedInventory = getSaleInventoryFromDate2ThruDate(delegator, id, createTime_fld0_value,
				createTime_fld1_value);
			BigDecimal estimatedFreeze = (BigDecimal) estimatedInventory.get("estimatedFreeze");
			BigDecimal estimatedRefrigerate = (BigDecimal) estimatedInventory.get("estimatedRefrigerate");
			BigDecimal estimatedConstant = (BigDecimal) estimatedInventory.get("estimatedConstant");
			BigDecimal estimatedNormal = (BigDecimal) estimatedInventory.get("estimatedNormal");
			// 预测销售库存小计;
			BigDecimal estimatedTotal = estimatedFreeze.add(estimatedRefrigerate).add(estimatedConstant)
				.add(estimatedNormal);
			// 销售库位与空余库位占比

			BigDecimal availableTotalDivide = availableTotal;
			BigDecimal estimatedTotalDivide = estimatedTotal;

			if (availableTotalDivide.compareTo(BigDecimal.ONE) < 0) {
			    availableTotalDivide = BigDecimal.ONE;
			}
			if (availableTotalDivide.doubleValue() < 1) {
			    availableTotalDivide = BigDecimal.ONE;
			}
			if (estimatedTotalDivide.compareTo(BigDecimal.ONE) < 0) {
			    estimatedTotalDivide = BigDecimal.ONE;
			}

			if (estimatedTotalDivide.doubleValue() < 1) {
			    estimatedTotalDivide = BigDecimal.ONE;
			}

			// 总计销售库位与总计空余库位占比
			BigDecimal estimatedTotalRate = estimatedTotal.divide(availableTotalDivide, 2, BigDecimal.ROUND_HALF_UP)
				.multiply(new BigDecimal(100));

			// 总计空余库位与总计销售库位占比
			BigDecimal estimatedTotalRate2 = availableTotalDivide
				.divide(estimatedTotalDivide, 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
			map.put("warehouseId", id);
			map.put("warehouseName", warehouseName);
			// map.put("estimatedDivAvailableRate", estimatedDivAvailableRate);
			// 已使用
			map.put("physicalFreeze", physicalFreeze);
			map.put("physicalRefrigerate", physicalRefrigerate);
			map.put("physicalConstant", physicalConstant);
			map.put("physicalNormal", physicalNormal);
			
			
			map.put("physicalTotal", physicalTotal);

			map.put("availableTotal", availableTotal);
			map.put("estimatedTotal", estimatedTotal);
			map.put("estimatedTotalRate", estimatedTotalRate);

			map.put("estimatedTotalRate2", estimatedTotalRate2);
			
			//全温区
			map.put("estimatedFreeze", estimatedFreeze);
			map.put("estimatedRefrigerate", estimatedRefrigerate);
			map.put("estimatedConstant", estimatedConstant);
			map.put("estimatedNormal", estimatedNormal);
			
			BigDecimal availableFreeze = buildingFreeze.subtract(physicalFreeze);
			BigDecimal availableRefrigerate = buildingRefrigerate.subtract(physicalRefrigerate);
			BigDecimal availableConstant = buildingConstant.subtract(physicalConstant);
			BigDecimal availableNormal = buildingNormal.subtract(physicalNormal);
			
			map.put("availableFreeze", availableFreeze);
			map.put("availableRefrigerate", availableRefrigerate);
			map.put("availableConstant", availableConstant);
			map.put("availableNormal", availableNormal);
			
			
			map.put("estimatedFreezeRate", CrmReportServices.estimatedOtherRate(estimatedFreeze, availableFreeze));
			map.put("estimatedRefrigerateRate", CrmReportServices.estimatedOtherRate(estimatedRefrigerate,availableRefrigerate));
			map.put("estimatedConstantRate", CrmReportServices.estimatedOtherRate(estimatedConstant,availableConstant));
			map.put("estimatedNormalRate", CrmReportServices.estimatedOtherRate(estimatedNormal,availableNormal));

			
			map.put("estimatedFreezeRate2", CrmReportServices.estimatedOtherRate2(estimatedFreeze, availableFreeze));
			map.put("estimatedRefrigerateRate2", CrmReportServices.estimatedOtherRate2(estimatedRefrigerate,availableRefrigerate));
			map.put("estimatedConstantRate2", CrmReportServices.estimatedOtherRate2(estimatedConstant,availableConstant));
			map.put("estimatedNormalRate2", CrmReportServices.estimatedOtherRate2(estimatedNormal,availableNormal));
			
			
			map.put("estimatedFreezeRateNo", new BigDecimal(200).subtract((BigDecimal)map.get("estimatedFreezeRate")));
			map.put("estimatedRefrigerateRateNo", new BigDecimal(200).subtract((BigDecimal)map.get("estimatedRefrigerateRate")));
			map.put("estimatedConstantRateNo", new BigDecimal(200).subtract((BigDecimal)map.get("estimatedConstantRate")));
			map.put("estimatedNormalRateNo", new BigDecimal(200).subtract((BigDecimal)map.get("estimatedNormalRate")));
			
			
			salesDataListByWarehouse.add(map);

			cautionSalesDataMap.put("warehouseId", id);
			cautionSalesDataMap.put("warehouseName", warehouseName);

			// 已使用
			cautionSalesDataMap.put("physicalFreeze", physicalFreeze);
			cautionSalesDataMap.put("physicalRefrigerate", physicalRefrigerate);
			cautionSalesDataMap.put("physicalConstant", physicalConstant);
			cautionSalesDataMap.put("physicalNormal", physicalNormal);
			cautionSalesDataMap.put("physicalTotal", physicalTotal);

			cautionSalesDataMap.put("availableTotal", availableTotal);
			cautionSalesDataMap.put("estimatedTotal", estimatedTotal);
			cautionSalesDataMap.put("estimatedTotalRate", estimatedTotalRate);
			cautionSalesDataMap.put("estimatedTotalRate2", estimatedTotalRate2);
			cautionSalesDataMap.put("estimatedTotalRateNo", new BigDecimal(200).subtract(estimatedTotalRate));


			cautionSalesDataListByWarehouse.add(cautionSalesDataMap);


		    } // 循环结束

		    Map<String, Object> workMap = FastMap.newInstance();

		    workMap.put("customerListByProductCategory",CrmReportServices.customerNewListByProductCategory(delegator, baseIdList, ownerUserId));
		   
		    
		    
		    Collections.sort(salesDataListByWarehouse, new Comparator<Map>() {
				@Override
				public int compare(Map obj1, Map obj2) {
				    int i = ((BigDecimal) obj2.get("estimatedTotalRate")).compareTo((BigDecimal) obj1.get("estimatedTotalRate"));// 倒序
				    return i;// 这里返回的只是一个int类型的数据
				}
			    });
		    workMap.put("salesDataListByWarehouse", salesDataListByWarehouse);// 汇总

		    Map allMap = FastMap.newInstance();
		    allMap.put("warehouseId", "_NA_");
		    allMap.put("warehouseName", "汇总");
		    BigDecimal physicalFreeze = BigDecimal.ZERO;
		    BigDecimal physicalRefrigerate = BigDecimal.ZERO;
		    BigDecimal physicalConstant = BigDecimal.ZERO;
		    BigDecimal physicalNormal = BigDecimal.ZERO;
		    BigDecimal physicalTotal = BigDecimal.ZERO;
		    BigDecimal availableFreeze = BigDecimal.ZERO;
		    BigDecimal estimatedFreeze = BigDecimal.ZERO;
		    BigDecimal estimatedFreezeRate = BigDecimal.ZERO;
		    BigDecimal availableRefrigerate = BigDecimal.ZERO;
		    BigDecimal estimatedRefrigerate = BigDecimal.ZERO;
		    BigDecimal estimatedRefrigerateRate = BigDecimal.ZERO;
		    BigDecimal availableConstant = BigDecimal.ZERO;
		    BigDecimal estimatedConstant = BigDecimal.ZERO;
		    BigDecimal estimatedConstantRate = BigDecimal.ZERO;
		    BigDecimal availableNormal = BigDecimal.ZERO;
		    BigDecimal estimatedNormal = BigDecimal.ZERO;
		    BigDecimal estimatedNormalRate = BigDecimal.ZERO;
		    BigDecimal availableTotal = BigDecimal.ZERO;
		    BigDecimal estimatedTotal = BigDecimal.ZERO;
		    BigDecimal estimatedTotalRate = BigDecimal.ZERO;
		    for (Object item : salesDataListByWarehouse) {
			Map info = (Map) item;
			if (UtilValidate.isNotEmpty(info.get("physicalFreeze"))) {
			    physicalFreeze = physicalFreeze.add((BigDecimal) info.get("physicalFreeze"));
			}
			if (UtilValidate.isNotEmpty(info.get("physicalRefrigerate"))) {
			    physicalRefrigerate = physicalRefrigerate.add((BigDecimal) info.get("physicalRefrigerate"));
			}
			if (UtilValidate.isNotEmpty(info.get("physicalConstant"))) {
			    physicalConstant = physicalConstant.add((BigDecimal) info.get("physicalConstant"));
			}
			if (UtilValidate.isNotEmpty(info.get("physicalNormal"))) {
			    physicalNormal = physicalNormal.add((BigDecimal) info.get("physicalNormal"));
			}
			if (UtilValidate.isNotEmpty(info.get("physicalTotal"))) {
			    physicalTotal = physicalTotal.add((BigDecimal) info.get("physicalTotal"));
			}
			if (UtilValidate.isNotEmpty(info.get("availableFreeze"))) {
			    availableFreeze = availableFreeze.add((BigDecimal) info.get("availableFreeze"));
			}
			if (UtilValidate.isNotEmpty(info.get("estimatedFreeze"))) {
			    estimatedFreeze = estimatedFreeze.add((BigDecimal) info.get("estimatedFreeze"));
			}
			if (UtilValidate.isNotEmpty(info.get("estimatedFreezeRate"))) {
			    estimatedFreezeRate = estimatedFreezeRate.add((BigDecimal) info.get("estimatedFreezeRate"));
			}
			if (UtilValidate.isNotEmpty(info.get("availableRefrigerate"))) {
			    availableRefrigerate = availableRefrigerate.add((BigDecimal) info.get("availableRefrigerate"));
			}
			if (UtilValidate.isNotEmpty(info.get("estimatedRefrigerate"))) {
			    estimatedRefrigerate = estimatedRefrigerate.add((BigDecimal) info.get("estimatedRefrigerate"));
			}
			if (UtilValidate.isNotEmpty(info.get("estimatedRefrigerateRate"))) {
			    estimatedRefrigerateRate = estimatedRefrigerateRate
				    .add((BigDecimal) info.get("estimatedRefrigerateRate"));
			}
			if (UtilValidate.isNotEmpty(info.get("availableConstant"))) {
			    availableConstant = availableConstant.add((BigDecimal) info.get("availableConstant"));
			}
			if (UtilValidate.isNotEmpty(info.get("estimatedConstant"))) {
			    estimatedConstant = estimatedConstant.add((BigDecimal) info.get("estimatedConstant"));
			}
			if (UtilValidate.isNotEmpty(info.get("estimatedConstantRate"))) {
			    estimatedConstantRate = estimatedConstantRate.add((BigDecimal) info.get("estimatedConstantRate"));
			}
			if (UtilValidate.isNotEmpty(info.get("availableNormal"))) {
			    availableNormal = availableNormal.add((BigDecimal) info.get("availableNormal"));
			}
			if (UtilValidate.isNotEmpty(info.get("estimatedNormal"))) {
			    estimatedNormal = estimatedNormal.add((BigDecimal) info.get("estimatedNormal"));
			}
			if (UtilValidate.isNotEmpty(info.get("estimatedNormalRate"))) {
			    estimatedNormalRate = estimatedNormalRate.add((BigDecimal) info.get("estimatedNormalRate"));
			}
			if (UtilValidate.isNotEmpty(info.get("availableTotal"))) {
			    availableTotal = availableTotal.add((BigDecimal) info.get("availableTotal"));
			}
			if (UtilValidate.isNotEmpty(info.get("estimatedTotal"))) {
			    estimatedTotal = estimatedTotal.add((BigDecimal) info.get("estimatedTotal"));
			}
		    }
//		    if (availableTotal.doubleValue() < 1) {
//			availableTotal = BigDecimal.ONE;
//		    }
		    BigDecimal availableTotalDivie = availableTotal;
		    if (availableTotalDivie.doubleValue() < 1) {
			availableTotalDivie = BigDecimal.ONE;
		    }

		    estimatedTotalRate = estimatedTotal.divide(availableTotalDivie, 2, BigDecimal.ROUND_HALF_UP)
			    .multiply(new BigDecimal(100));
		    allMap.put("physicalFreeze", physicalFreeze);
		    allMap.put("physicalRefrigerate", physicalRefrigerate);
		    allMap.put("physicalConstant", physicalConstant);
		    allMap.put("physicalNormal", physicalNormal);
		    allMap.put("physicalTotal", physicalTotal);

		    allMap.put("availableFreeze", availableFreeze);
		    allMap.put("estimatedFreeze", estimatedFreeze);
		    allMap.put("estimatedFreezeRate", estimatedFreezeRate);

		    allMap.put("availableRefrigerate", availableRefrigerate);
		    allMap.put("estimatedRefrigerate", estimatedRefrigerate);
		    allMap.put("estimatedRefrigerateRate", estimatedRefrigerateRate);

		    allMap.put("availableConstant", availableConstant);
		    allMap.put("estimatedConstant", estimatedConstant);
		    allMap.put("estimatedConstantRate", estimatedConstantRate);

		    allMap.put("availableNormal", availableNormal);
		    allMap.put("estimatedNormal", estimatedNormal);
		    allMap.put("estimatedNormalRate", estimatedNormalRate);

		    allMap.put("availableTotal", availableTotal);
		    allMap.put("estimatedTotal", estimatedTotal);
		    allMap.put("estimatedTotalRate", estimatedTotalRate);

		    workMap.put("salesDataListByWarehouseHuizong", allMap);
		    workMap.put("cautionSalesDataListByWarehouse", cautionSalesDataListByWarehouse);

		    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		    String fromDateDateFormat = simpleDateFormat.format(createTime_fld0_value);
		    String thruDateDateFormat = simpleDateFormat.format(createTime_fld1_value);
		    workMap.put("fromDate", fromDateDateFormat);
		    workMap.put("thruDate", thruDateDateFormat);

		    result.put("workMap", workMap);
		    Debug.log("getActiveBusinessReport == end");
		} catch (Exception e) {
		    Debug.logError(e.getMessage(), module);
		}
		return result;
	    }
	
	
	// 获取单个仓库的销售商机数量
    private static Map getSaleInventoryFromDate2ThruDate(Delegator delegator, String inventoryId, Timestamp fromDate,
	    Timestamp thruDate) throws Exception {

	Map result = new HashMap();

	BigDecimal estimatedFreezeTotal = BigDecimal.ZERO;
	BigDecimal estimatedRefrigerateTotal = BigDecimal.ZERO;
	BigDecimal estimatedConstantTotal = BigDecimal.ZERO;
	BigDecimal estimatedNormalTotal = BigDecimal.ZERO;

	Debug.log("inventoryId =" + inventoryId);
	// 销售人员 的商机
	ArrayList<EntityCondition> mainCond = new ArrayList<>();
	
	//, "5"
	mainCond.add(
		EntityCondition.makeCondition("statusId", EntityOperator.IN, UtilMisc.toList("0", "1", "2", "3", "4")));

	mainCond.add(EntityCondition.makeCondition("inventoryId", EntityOperator.EQUALS, inventoryId));
	mainCond.add(EntityCondition.makeCondition("typeId", EntityOperator.EQUALS, "1"));
	mainCond.add(EntityCondition.makeCondition("removed", EntityOperator.EQUALS, "N"));
	if (UtilValidate.isNotEmpty(fromDate)) {
	    mainCond.add(EntityCondition.makeCondition("createTime", EntityOperator.GREATER_THAN_EQUAL_TO, fromDate));

	}
	if (UtilValidate.isNotEmpty(thruDate)) {
	    mainCond.add(EntityCondition.makeCondition("createTime", EntityOperator.LESS_THAN, thruDate));
	}
	List<GenericValue> crmBusinessItemList = delegator.findList("CrmBusinessItemByStatusId",
		EntityCondition.makeCondition(mainCond, EntityOperator.AND), null, null, null, true);

//	long followupLong = 100L;
//	BigDecimal	followupBigDecimal = BigDecimal.valueOf(followupLong).divide(new BigDecimal(100), 2,
//			BigDecimal.ROUND_HALF_UP);
//	
	for (GenericValue crmBusinessItem : crmBusinessItemList) {
	    String businessId = crmBusinessItem.getString("businessId");
	   

	  
	    	
	    

	    Debug.log("businessId =" + businessId);

	    // 冷冻
	    BigDecimal estimatedFreezeBigDecimal = BigDecimal.ZERO;
	    BigDecimal estimatedFreeze = crmBusinessItem.getBigDecimal("estimatedFreeze");
	    if (UtilValidate.isNotEmpty(estimatedFreeze)) {
		
		estimatedFreezeTotal = estimatedFreezeTotal.add(estimatedFreeze);
	    }
	    // 冷藏
	    BigDecimal estimatedRefrigerateBigDecimal = BigDecimal.ZERO;
	    BigDecimal estimatedRefrigerate = crmBusinessItem.getBigDecimal("estimatedRefrigerate");
	    if (UtilValidate.isNotEmpty(estimatedRefrigerate)) {
		

		estimatedRefrigerateTotal = estimatedRefrigerateTotal.add(estimatedRefrigerate);
	    }
	    // 恒温
	    BigDecimal estimatedConstantBigDecimal = BigDecimal.ZERO;
	    BigDecimal estimatedConstant = crmBusinessItem.getBigDecimal("estimatedConstant");
	    if (UtilValidate.isNotEmpty(estimatedConstant)) {
		

		estimatedConstantTotal = estimatedConstantTotal.add(estimatedConstant);
	    }
	    // 常温
	    BigDecimal estimatedNormalBigDecimal = BigDecimal.ZERO;
	    BigDecimal estimatedNormal = crmBusinessItem.getBigDecimal("estimatedNormal");
	    if (UtilValidate.isNotEmpty(estimatedNormal)) {
		
		estimatedNormalTotal = estimatedNormalTotal.add(estimatedNormal);
	    }

	}

	result.put("crmBusinessItemListSize", crmBusinessItemList.size());

	result.put("estimatedFreeze", estimatedFreezeTotal);
	result.put("estimatedRefrigerate", estimatedRefrigerateTotal);
	result.put("estimatedConstant", estimatedConstantTotal);
	result.put("estimatedNormal", estimatedNormalTotal);
	Debug.log("result =" + result);
	return result;

    }

}
